GITHUB := https://github.com

ARCHIVE_EXT := tar.gz
extract = tar -zxmf '${1}' ${2}

# repo
METALANG_REPO := Hirrolot/metalang99

# version
METALANG_VERSION := 1.13.3

# headers
METALANG_HEADERS := metalang99.h
METALANG_HEADERS_ALL := ${METALANG_HEADERS} metalang99/
#
# archive
METALANG_ARCHIVE_EXTRACTED := metalang99-${METALANG_VERSION}
METALANG_ARCHIVE := ${METALANG_ARCHIVE_EXTRACTED}.${ARCHIVE_EXT}

# url
METALANG_URL := ${GITHUB}/${METALANG_REPO}/archive/v${METALANG_VERSION}.${ARCHIVE_EXT}

HEADERS_ALL  := ${METALANG_HEADERS_ALL}
ARCHIVES_ALL := ${METALANG_ARCHIVE}

DOWNLOADER ?= curl
# download function should downlad file ${1} from url ${2}
ifeq (${DOWNLOADER},curl)
download = curl -Lo '${1}' -- '${2}'
else # if curl
ifeq  (${DOWNLOADER},wget)
download = wget -O '${1}' -- '${2}'
endif # if wget
endif # if curl

.PHONY: all
all: metalang

.PHONY: metalang
metalang: ${METALANG_HEADERS}

${METALANG_HEADERS}: ${METALANG_ARCHIVE}
	$(call extract,${<},\
		$(foreach _,\
			${METALANG_HEADERS_ALL},\
			${METALANG_ARCHIVE_EXTRACTED}/include/${_}))
	mv -v -- '${METALANG_ARCHIVE_EXTRACTED}/include'/* .
	rmdir -p -- '${METALANG_ARCHIVE_EXTRACTED}/include'

define download_rule
$${${1}_ARCHIVE}:
	$$(call download,$${@},$${${1}_URL})
endef

$(eval $(call download_rule,METALANG))

.PHONY: clean
clean:
	rm -rf -- ${HEADERS_ALL} ${ARCHIVES_ALL}
